package com.gzzyy.facerecognition

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.fastjson.JSON
import com.bumptech.glide.Glide
import com.gzzyy.facerecognition.bean.PersonInfo
import java.io.File

class ResultActivity : FragmentActivity() {
    companion object {
        const val FilePath = "file_path"
        const val Result = "result"
    }

    private var filePath: String? = null
    private var jsonString: String? = null
    private lateinit var viewPager: ViewPager2
    private lateinit var personCountTv: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.BOARD == Board) {
            setContentView(R.layout.activity_result_rk)
        } else {
            setContentView(R.layout.activity_result)
        }
        val imageView = findViewById<ImageView>(R.id.photoImg)
        filePath = intent.getStringExtra(FilePath)
        if (filePath != null) {
            val file = File(filePath)
            Glide.with(baseContext).load(file).into(imageView)
        }
        personCountTv = findViewById<TextView>(R.id.personCountTv)

        viewPager = findViewById(R.id.viewPager)
        jsonString = intent.getStringExtra(Result)

        if (jsonString != null) {
            Log.e("test", "json = $jsonString")
            val personInfoList = JSON.parseArray(jsonString, PersonInfo::class.java)
            if (!personInfoList.isNullOrEmpty()) {
                if (personInfoList.size == 1 && personInfoList[0].attributes == null) {
                    personCountTv.text = "未识别到人体"
                } else {
                    personCountTv.text = "识别到${personInfoList.size}人"
                }
            }
            val pageAdapter = ResultPageAdapter(this, personInfoList)
            viewPager.adapter = pageAdapter
        }
    }
}